package com.youdoneed.community.forum.web;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.youdoneed.community.common.vo.PageData;
import com.youdoneed.community.common.vo.ResultMessage;
import com.youdoneed.community.forum.entity.Forumreport;
import com.youdoneed.community.forum.service.ForumreportService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 举报模块控制器
 *
 * @author lid
 * @date 2017.6.9
 */
@SuppressWarnings("unchecked")
@RestController
@RequestMapping(value = "/forumreport", method = {RequestMethod.GET, RequestMethod.POST})
@Api(value = "举报模块控制器", description = "举报模块相关操作")
public class ForumreportController {
    private static final Logger logger = LoggerFactory.getLogger(ForumreportController.class);

    @Autowired
    ForumreportService forumreportService;

    /**
     * 保存举报信息
     *
     * @param forumreport
     * @return ResultMessage
     * @author lid
     * @date 2017.6.9
     */
    @ResponseBody
    @RequestMapping(value = "/saveForumreport", method = {RequestMethod.GET, RequestMethod.POST})
    @ApiOperation(notes = "保存举报信息", value = "保存举报信息")
    public ResultMessage<?> saveForumreport(@RequestBody Forumreport forumreport) {
        logger.info("saveForumreport" + forumreport.getForumMsgId() +
                "," + forumreport.getForumReportId() + "," + forumreport.getForumReportReason() +
                "," + forumreport.getForumReportReasonId() + "," + forumreport.getReportedUserId()
                + "," + forumreport.getReportUserId() + "," + forumreport.getCreateDate());
        try {
            int result = forumreportService.saveForumreport(forumreport);
            logger.info("result:" + result);
            return ResultMessage.newSuccess("保存举报信息成功");
        } catch (Exception e) {
            return ResultMessage.newFailure("保存举报信息失败");
        }
    }

    /**
     * 删除举报信息
     *
     * @param forumreport
     * @return ResultMessage
     * @author lid
     * @date 2017.6.9
     */
    @ResponseBody
    @RequestMapping(value = "/deleteForumreport", method = {RequestMethod.GET, RequestMethod.POST})
    @ApiOperation(notes = "删除举报信息", value = "删除举报信息")
    public ResultMessage<?> deleteForumreport(String forumReportId) {
        logger.info("deleteForumreport");
        try {
            forumreportService.deleteForumreport(forumReportId);
            return ResultMessage.newSuccess("删除举报信息成功");
        } catch (Exception e) {
            return ResultMessage.newFailure("删除举报信息失败");
        }
    }

    /**
     * 查找举报信息
     *
     * @param forumreport
     * @return ResultMessage
     * @author lid
     * @date 2017.6.9
     */
    @ResponseBody
    @RequestMapping(value = "/findByForumreportId", method = {RequestMethod.GET, RequestMethod.POST})
    @ApiOperation(notes = "查找举报信息", value = "查找举报信息")
    public ResultMessage<Forumreport> findByForumreportId(String forumreportId) {
        logger.info("findByForumreportId");
        try {
            Forumreport forumreport = forumreportService.findByForumreportId(forumreportId);
            return ResultMessage.newSuccess("查找举报信息成功").setData(forumreport);
        } catch (Exception e) {
            return ResultMessage.newFailure("查找举报信息失败");
        }
    }


    /**
     * 通过msgId查找当前帖子的所有举报信息
     *
     * @param forumreport
     * @return ResultMessage
     * @author lid
     * @date 2017.6.9
     */
    @ResponseBody
    @RequestMapping(value = "/findForumreportByMsgId", method = {RequestMethod.GET, RequestMethod.POST})
    @ApiOperation(notes = "通过msgId查找当前帖子的所有举报信息", value = "通过msgId查找当前帖子的所有举报信息")
    public ResultMessage<List<Forumreport>> findForumreportByMsgId(String forumMsgId) {
        logger.info("findForumreportByMsgId");
        try {
            List<Forumreport> listForumreport = forumreportService.findForumreportByMsgId(forumMsgId);
            return ResultMessage.newSuccess("通过msgId查找当前帖子的所有举报信息成功").setData(listForumreport);
        } catch (Exception e) {
            return ResultMessage.newFailure("通过msgId查找当前帖子的所有举报信息失败");
        }
    }

    /**
     * 通过举报原因和创建时间查询被举报帖子
     *
     * @param forumReportReason
     * @param createDateS
     * @param createDateE
     * @param limit
     * @param offset
     * @author lixs
     * @date 2017 6.13
     */
    @RequestMapping(value = "/querySelectReportForumMsg", method = {RequestMethod.GET, RequestMethod.POST})
    @ApiOperation(notes = "根据条件查询被举报帖子", value = "根据条件查询被举报帖子")
    public ResultMessage<PageData> querySelectReportForumMsg(String forumReportReason, String createDateS, String createDateE, Integer limit, Integer offset) {
        logger.info("根据举报类型" + forumReportReason + "时间段" + createDateS + "-" + createDateE + "查询被举报帖子信息");
        List<Forumreport> ReportList = forumreportService.getSelectReportForumMsg(forumReportReason, createDateS, createDateE);
        List<Forumreport> forumReportMsgList = forumreportService.getSelectReportForumMsgByPage(forumReportReason, createDateS, createDateE, limit, offset);
        if (null == forumReportMsgList) {
            return ResultMessage.newFailure("获取被举报帖子失败！");
        }
//		Map<String, Object> mapResult = new HashMap<String, Object>();
//		mapResult.put("rows", forumReportMsgList);
//		
//		logger.info("获取被举报帖子总数");
//		int total = ReportList.size();
//		logger.info("total:"+total);
//		mapResult.put("total", total);
//		logger.info("mapResult:"+mapResult);
        PageData page = new PageData();
        page.setRows(forumReportMsgList);
        page.setTotal(ReportList.size());
        return ResultMessage.newSuccess("获取被举报帖子成功！").setData(page);
    }

    /**
     * 后台处理被举报帖子
     *
     * @param forumreportId
     * @param forumHandle
     * @author lixs
     * @Date 2017.8.8
     */
    @RequestMapping(value = "/dealReportForumMessage", method = {RequestMethod.GET, RequestMethod.POST})
    @ApiOperation(notes = "后台处理被举报帖子", value = "后台处理被举报帖子")
    public ResultMessage<?> dealReportForumMessage(String forumMsgId, String forumHandle, String handlePerson) {
        logger.info("后台处理被举报帖子");
        int result = forumreportService.dealReportForumMessage(forumMsgId, forumHandle, handlePerson);
        if (result == 0) {
            return ResultMessage.newFailure("该帖子已被处理但未删除");
        }
        return ResultMessage.newSuccess("该帖子已被处理并删除!").setData(result);
    }
}
